package com.rdc.dictionary.start;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.rdc.dictionary.R;
import com.rdc.dictionary.application.App;
import com.rdc.dictionary.bean.Word;
import com.rdc.dictionary.bean.WordSearch;
import com.rdc.dictionary.daily.DailyActivity;
import com.rdc.dictionary.search.SearchActivity;
import com.rdc.dictionary.vocabulary.VocabularyActivity;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{

	private Button mSearch;
	private Button mVocabulary;
	private Button mDaily;

	@Override protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

//		//测试时删除清空数据库
//		DataSupport.deleteAll(Word.class);
//		DataSupport.deleteAll(WordSearch.class);


		//检查权限
		if (ContextCompat.checkSelfPermission(App.sContext, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED)
		{
			ActivityCompat.requestPermissions(MainActivity.this, new
					String[]{ Manifest.permission.INTERNET }, 1);
		}

		if (ContextCompat.checkSelfPermission(App.sContext, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
		{
			ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, 1);
		}


		if (ContextCompat.checkSelfPermission(App.sContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
		{
			ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1);
		}


		if (ContextCompat.checkSelfPermission(App.sContext, Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS) != PackageManager.PERMISSION_GRANTED)
		{
			ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS }, 1);
		}



		mSearch = findViewById(R.id.bt_search);
		mVocabulary = findViewById(R.id.bt_vocabulary);
		mDaily = findViewById(R.id.bt_daily);

		mSearch.setOnClickListener(this);
		mVocabulary.setOnClickListener(this);
		mDaily.setOnClickListener(this);



	}

	/**
	 *
	 * 活动切换
	*/
	@Override public void onClick(View v)
	{
		switch(v.getId())
		{
		case R.id.bt_search:

			Intent intent1 = new Intent(App.sContext , SearchActivity.class);
			startActivity(intent1);

			Toast.makeText(App.sContext , "请输入搜索单词", Toast.LENGTH_SHORT).show();

			break;

		case R.id.bt_vocabulary:

			Intent intent2 = new Intent(App.sContext , VocabularyActivity.class);
			startActivity(intent2);

			break;

		case R.id.bt_daily:

			Intent intent3 = new Intent(App.sContext , DailyActivity.class);
			startActivity(intent3);

			break;

		}
	}
}